var COS = require('cos-wx-sdk-v5.js');
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

//请求时加上加载框
let ajatime=0;
//请求
const request=(params)=>{
  ajatime++;
  wx.showLoading({
    title:'加载中...',
    mask:true
  });
  return new Promise((resolve, reject)=>{
    wx.request({
      ...params,
      success(res){
        resolve(res.data);
      },
      fail(err){
        reject(err);
      },
      complete:()=>{
        ajatime--;
        if(ajatime===0){
          wx.hideLoading();
        }
      }
    })
  })
};

/*const showtoas=(parm)={
  return new Promise((resolve, reject)=>{
    wx.request({
      ...parm,
      success(res){
        resolve(res.data);
      },
      fail(err){
        reject(err);
      }
    })
  })
}*/

//模态框
const showtoas=(params)=>{
  return new Promise((resolve, reject)=>{
    wx.showModal({
      ...params,
      success(res){
        resolve(res);
      },
      fail(err){
        reject(err);
      }
    });
  })
};

//消息提示
const showta=(params)=>{
  return new Promise((resolve, reject)=>{
    wx.showToast({
      ...params,
      success(res){
        resolve(res);
      },
      fail(err){
        reject(err);
      }
    });
  })
};

//微信登入
const wxlogin=()=>{
  return new Promise((resolve, reject)=>{
    wx.login({
      success(res){
        resolve(res);
      },
      fail(err){
        reject(err);
      }
    });
  })
};

//生成临时秘钥 没nodejs接口
/*var cos = new COS({
  // ForcePathStyle: true, // 如果使用了很多存储桶，可以通过打开后缀式，减少配置白名单域名数量，请求时会用地域域名
  getAuthorization: function (options, callback) {
    // 异步获取临时密钥
    wx.request({
      url: 'https://example.com/server/sts.php',
      data: {
        bucket: options.Bucket,
        region: options.Region,
      },
      dataType: 'json',
      success: function (result) {
        var data = result.data;
        var credentials = data && data.credentials;
        if (!data || !credentials) return console.error('credentials invalid');
        callback({
          TmpSecretId: credentials.tmpSecretId,
          TmpSecretKey: credentials.tmpSecretKey,
          XCosSecurityToken: credentials.sessionToken,
          // 建议返回服务器时间作为签名的开始时间，避免用户浏览器本地时间偏差过大导致签名错误
          StartTime: data.startTime, // 时间戳，单位秒，如：1580000000
          ExpiredTime: data.expiredTime, // 时间戳，单位秒，如：1580000900
        });
      }
    });
  }
});*/

// SECRETID 和 SECRETKEY请登录
var cos = new COS({
  SecretId: 'xxxxxx',
  SecretKey: 'xxxx',
});
//储存库名字
let Bucket='1301165591'
//地域
let Region='ap-guangzhou'
//文件上传到云存储
const  cosupimg=(filename,filePath)=>{
  return new Promise((resolve,reject)=>{
    cos.postObject({
      Bucket: Bucket,
      Region: Region,
      Key: 'applet of WeChat/shop/' + filename,
      FilePath: filePath,
    }, function (err, data) {
      //判断返回的结果
      if(data!=null){
        resolve(data);
      }else{
        reject(err);
      }
    });
  })
}

//删除云存储图片
const decosimg=(filename)=>{
  return new Promise((resolve, reject)=>{
    cos.deleteObject({
      Bucket: Bucket,
      Region: Region,
      Key: 'applet of WeChat/shop/' + filename,
    }, function(err, data) {
      if(data!=null){
        resolve(data);
      }else{
        reject(err);
      }
    });
  });
}

module.exports = {
  formatTime,
  request,
  showtoas,
  wxlogin,
  cosupimg,
  decosimg,
  showta
}
